package com.woniuxy.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.entity.Order;
import com.woniuxy.service.mapper.OrderMapper;
import com.woniuxy.service.service.OrderService;
import com.woniuxy.service.utils.PageMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    @Override
    public PageMessage all(int page, int size) {
        //设置分页
        PageHelper.startPage(page, size);
        List<Order> all = orderMapper.all();
        PageInfo<Order> pageInfo = PageInfo.of(all);
        // 封装信息
        PageMessage pageMessage = new PageMessage();
        pageMessage.setCurrentPage(page);
        pageMessage.setSize(size);
        pageMessage.setTotalPage(pageInfo.getPages());
        pageMessage.setTotal(pageInfo.getTotal());
        pageMessage.setData(all);
        return pageMessage;
    }

    @Override
    public boolean turn(int orderId, int escortId) {
        int result = orderMapper.turn(orderId, escortId);
        return result > 0 ? true : false;
    }

    @Override
    public boolean close(int orderId) {
        int result = orderMapper.close(orderId);
        return result > 0 ? true : false;
    }

    @Override
    public PageMessage findbyNoAndNameNndStatus(int page, int size, String no, String name, String escort, int status) {
        //设置分页
        PageHelper.startPage(page, size);
        List<Order> result=null;
       if(no!=null&&status!=15&&name!=null&&escort!=null){
           List<Order> all=orderMapper.findbynoandnameandescortandstatus(no,name,escort,status);
           result=all;
       }
        if(no!=null&&status!=15&&name==null&&escort!=null){
            List<Order> all=orderMapper.findbynoandescortandstatus(no,escort,status);
            result=all;
        }
        if(no!=null&&status!=15&&name!=null&&escort==null){
            List<Order> all=orderMapper.findbynoandnameandstatus(no,escort,status);
            result=all;
        }
        if(no!=null&&status!=15&&name==null&&escort==null){
            List<Order> all=orderMapper.findbynoandstatus(no,status);
            result=all;
        }


        if(no==null&&status!=15&&name!=null&&escort!=null){
            List<Order> all=orderMapper.findbynameandescortandstatus(name,escort,status);
            result=all;
        }
        if(no==null&&status!=15&&name==null&&escort!=null){
            List<Order> all=orderMapper.findbynoandnameandescortandstatus(no,name,escort,status);
            result=all;
        }
        if(no==null&&status!=15&&name!=null&&escort==null){
            List<Order> all=orderMapper.findbynoandnameandstatus(no,name,status);
            result=all;
        }
        if(no==null&&status!=15&&name==null&&escort==null){
            List<Order> all=orderMapper.findbystatus(status);
            result=all;
        }


        if(no!=null&&status==15&&name!=null&&escort!=null){
            List<Order> all=orderMapper.findbynoandnameandescort(no,name,escort);
            result=all;
        }
        if(no!=null&&status==15&&name==null&&escort!=null){
            List<Order> all=orderMapper.findbynoandescortandstatus(no,escort,status);
            result=all;
        }
        if(no!=null&&status==15&&name!=null&&escort==null){
            List<Order> all=orderMapper.findbynoandnameandstatus(no,name,status);
            result=all;
        }
        if(no!=null&&status==15&&name==null&&escort==null){
            List<Order> all=orderMapper.findbyno(no);
            result=all;
        }


        if(no==null&&status==15&&name!=null&&escort!=null){
            List<Order> all=orderMapper.findbynameandescort(name,escort);
            result=all;
        }
        if(no==null&&status==15&&name==null&&escort!=null){
            List<Order> all=orderMapper.findbyescort(escort);
            result=all;
        }
        if(no==null&&status==15&&name!=null&&escort==null){
            List<Order> all=orderMapper.findbyname(name);
            result=all;
        }

        PageInfo<Order> pageInfo = PageInfo.of(result);
        // 封装信息
        PageMessage pageMessage = new PageMessage();
        pageMessage.setCurrentPage(page);
        pageMessage.setSize(size);
        pageMessage.setTotalPage(pageInfo.getPages());
        pageMessage.setTotal(pageInfo.getTotal());
        pageMessage.setData(result);
        return pageMessage;

    }

    @Override
    public List<Order> disabled(int disabledId) {
        return orderMapper.disabled(disabledId);
    }

    @Override
    public boolean closebydisabled(int orderId) {
        int result=orderMapper.closebydisabled(orderId);
        return result > 0 ? true : false;
    }

    @Override
    public List<Order> findbydisabled(String key, int disabledId) {
        return orderMapper.findbydisabled(key, disabledId);

    }

    @Override
    public List<Order> findbyescortId(String key, int escortId) {
        return orderMapper.findbyescortId(key,escortId);
    }

    @Override
    public boolean add(Order order) {
        //设置下单时间
       Timestamp date=new Timestamp(new Date().getTime());
        order.setOrderTime(date);
        //设置订单号
        long orderTime=date.getTime();
        String orderNo=orderTime+"";
        order.setNo(orderNo);
        //订单状态和回访状态前后端都能写，暂时写后端
        order.setStatus(0);
        order.setRevisitstatus(0);
        //下单后指定一个没有名字的人，方前端组件正常显示
        order.setEscortId(666);
        int result=orderMapper.add(order);
        return result > 0 ? true : false;
    }

    @Override
    public Order findbyid(int orderId) {
        return orderMapper.findbyid(orderId);
    }

    @Override
    public boolean pay(int orderId) {
        int result=orderMapper.pay(orderId);
        return result > 0 ? true : false;
    }

    @Override
    public boolean paybyno(String orderNo) {
        int result=orderMapper.paybyno(orderNo);
        return result > 0 ? true : false;
    }

    @Override
    public boolean setorderno(String orderNo,int orderId) {
        int result=orderMapper.setorderno(orderNo,orderId);
        return result > 0 ? true : false;
    }

    @Override
    public boolean revisit(int orderId) {
        int result=orderMapper.revisit(orderId);
        return result > 0 ? true : false;
    }

    @Override
    public void review(String review, int orderId) {
           orderMapper.review(review,orderId);
    }

    @Override
    public List<Order> escort(int escortId) {
        return orderMapper.escort(escortId);
    }

    @Override
    public boolean accept(int orderId) {
        int result=orderMapper.accept(orderId);
        return result > 0 ? true : false;
    }

    @Override
    public boolean refuse(int orderId) {
        int result=orderMapper.refuse(orderId);
        return result > 0 ? true : false;
    }
}
